package org.test.web.controller;

import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.text.csv.CsvReader;
import cn.hutool.core.text.csv.CsvUtil;
import cn.hutool.cron.CronUtil;
import cn.hutool.http.HttpUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.test.web.entity.TestBean;
import org.test.web.entity.UserLoginResponse;

import java.util.List;

@RestController
@RequestMapping("")
public class HelloController {
    @RequestMapping("")
    public UserLoginResponse hello(){
        //System.out.println("hello2");
        //String chineseZodiac = DateUtil.getChineseZodiac(1984);
        //System.out.println(chineseZodiac);
        //测试反射
//        User user = new User();
//        ReflectUtil.invoke(user,"setName","weiwei");
//        String name = user.getName();
//        System.out.println(name);

//        String s = DesensitizedUtil.mobilePhone("18606009183");
//
//        String next = ObjectId.next();

        //参数1为终端ID
        //参数2为数据中心ID
//        Snowflake snowflake = IdUtil.getSnowflake(2, 3);
//        long id = snowflake.nextId();
//
//        Console.log(id);

        //Object eval = ScriptUtil.eval("print('Script test!');");

        //String str = "aaa\\U4e2d\\u6587\\u111\\urtyu\\u0026";//编码测试
        //String res = UnicodeUtil.toString(str);

        UserLoginResponse userLoginResponse = new UserLoginResponse();
        UserLoginResponse.UserLoginResponseData userLoginResponseData = new UserLoginResponse.UserLoginResponseData();
        userLoginResponseData.setUserId("100023");
        userLoginResponseData.setHeadPic("img.jpg");
        userLoginResponseData.setUserName("100023");
        userLoginResponseData.setIfPwd("pwd");
        userLoginResponseData.setMobile("18606009183");
        userLoginResponseData.setToken("fdjsklfdsj234324jlfsd");
        userLoginResponseData.setNickName("100023");


        userLoginResponse.setCode("200");
        userLoginResponse.setMessage("登录成功");
        userLoginResponse.setData(userLoginResponseData);

        return userLoginResponse;
    }

    //读取CSV
    @GetMapping("/readcsv")
    public List<TestBean> readCsv(){
        CsvReader reader = CsvUtil.getReader();
        List<TestBean> read = reader.read(ResourceUtil.getUtf8Reader("test_bean.csv"), TestBean.class);
        System.out.println(read);
        return read;
    }

    /**
     * 测试定时任务
     * @return
     */
    @GetMapping("/startcron")
    public String startCron(){
        //CronUtil.setCronSetting(new Setting("cron.setting"));
        CronUtil.start();

        return "任务启动成功";
    }

    @GetMapping("/stopcron")
    public String stopCron(){
        //CronUtil.setCronSetting(new Setting("cron.setting"));
        CronUtil.stop();

        return "任务停止成功";
    }

    public String getUrl(){//执行一个网址

        String s = HttpUtil.get("https://www.baidu.com", 300);

        Console.log(s);
        return s;
    }
}
